<!-- 
  MIT License

  Copyright (c) 2023 小辉不会飞

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
-->
<!-- accessibleMap 可访问的地图 -->
<template>
  <div class="container">
    <main-header showBack :title="route.query.name"></main-header>
    <div class="main">
      <div id="map" class="map" tabindex="0"></div>
      <div class="btns flex flex-hc">
        <el-button @click="zoomOut">视图缩小</el-button>
        <el-button @click="zoomIn">视图放大</el-button>
      </div>
    </div>
    <main-footer></main-footer>
  </div>
</template>

<script setup>
// VUE相关
import { onMounted, reactive } from "vue";
import { useRoute } from "vue-router";
// 项目组件
import mainHeader from "@/components/mainHeader.vue";
import mainFooter from "@/components/mainFooter.vue";
// OL相关
import Map from "ol/Map";
import OSM from "ol/source/OSM";
import TileLayer from "ol/layer/Tile";
import View from "ol/View";
import "ol/ol.css";
// 依赖
import { loadVecTdt } from "@/common/loadTDT";

const route = useRoute(); // 路由对象

const ol = reactive({
  map: null,
});

onMounted(() => {
  // initMap(); // 国内osm无法访问，此处替换成天地图
  ol.map = loadVecTdt("map");
});

/**
 * 初始化地图
 */
const initMap = () => {
  // 初始化地图到指定DOM元素
  ol.map = new Map({
    layers: [
      new TileLayer({
        source: new OSM(),
      }),
    ], // 图层
    target: "map", // 目标DOM元素
    view: new View({
      center: [0, 0],
      zoom: 2,
    }), // 视图配置
  });
};

/**
 * 缩小视图
 */
const zoomOut = () => {
  let view = ol.map.getView();
  let zoom = view.getZoom();
  view.setZoom(zoom - 1);
};

/**
 * 放大视图
 */
const zoomIn = () => {
  let view = ol.map.getView();
  let zoom = view.getZoom();
  view.setZoom(zoom + 1);
};
</script>

<style scoped lang="scss">
#map {
  width: 100%;
  height: calc(100% - 60px);
}
#map:focus {
  outline: #4a74a8 solid 0.15em;
}
.btns {
  height: 60px;
  padding: 0 15px;
}
</style>
